package com.jason.java.lib;

import java.util.regex.Pattern;

/**
 * Description:Test
 *
 * @author jason
 * @create 2020/9/2 16:05
 */
public class HttpUrlMatcherTest {


  public static void main(String[] args) {

    String url1 = "123";
    String url2 = "http://123";
    String url3 = "http://www.baidu.com?a=10";
    String url4 = "http://www.baidu.com?a=10&b=4";
    String url5 = "http://www.baidu.com";
    String url6 = "https://m.3839.com/html/hykb-192.html?3236611";
    System.out.println(url1 +isValidHttpUrl(url1));
    System.out.println(url2 +isValidHttpUrl(url2));
    System.out.println(url3 +isValidHttpUrl(url3));
    System.out.println(url4 +isValidHttpUrl(url4));
    System.out.println(url5 +isValidHttpUrl(url5));
    System.out.println(url6 +isValidHttpUrl(url6));
  }

  private static final String HTTP_URL_REGEX = "^((ht|f)tps?):\\/\\/([\\w\\-]+(\\.[\\w\\-]+)*\\/)*[\\w\\-]+(\\.[\\w\\-]+)*\\/?(\\?([\\w\\-\\.,@?^=%&:\\/~\\+#]*)+)?";

  /**
   * 校验url是否是合法的地址
   * @param url
   * @return
   */
  public static boolean isValidHttpUrl(String url) {
    if(url == null || url.length() <= 0) {
      return false;
    }
    Pattern pattern = Pattern.compile(HTTP_URL_REGEX);
    return pattern.matcher(url).matches();
  }
}
